<!-- 消息登录 -->
<template>
  <view class="login">
    <van-loading color="#0094ff" text-color="#0094ff">加载中...</van-loading>
  </view>
</template>
<script setup lang="ts">
  import * as dd from 'dingtalk-jsapi'; // 此方式为整体加载，也可按需进行加载
  import { dingLogin } from '@/services/api/user/login';
  import { onLoad } from '@dcloudio/uni-app';
  import { ref } from 'vue';
  import { useAuthStore } from '@/store/modules/auth';
  import { ResultEnum } from '@/enums/httpEnum';
  import { Toast } from '@/utils/uniapi/prompt';
  import { router } from '@/utils/router';
  import config from '@/utils/config';
  import { isEmpty } from '@/utils/is';

  const authStore = useAuthStore();

  /** 登录 */
  const submit = async (code: string, instanceId: string, nodeId: string, taskId: string) => {
    dingLogin(code).then((res: any) => {
      if (res.data.code == ResultEnum.SUCCESS) {
        authStore.setToken(res.data.data.token);
        authStore.setUserInfo(res.data.data.sysUser);
        // router.reLaunch(config.route.home);
        // 跳转详情页面
        router.replaceAll(
          config.route.processDetail +
            '?instanceId=' +
            instanceId +
            '&nodeId=' +
            nodeId +
            '&taskId=' +
            taskId,
        );
      } else {
        Toast(res.data.msg);
      }
    });
  };

  const code = ref('');

  onLoad(async (options: any) => {
    await dd.runtime.permission.requestAuthCode({
      corpId: options.corpId, // 企业id
      onSuccess: function (info: any) {
        code.value = info.code;
      },
    });
    await submit(
      code.value,
      options.instanceId,
      options.nodeId,
      isEmpty(options.taskId) ? 'undefined' : options.taskId,
    );
  });
</script>
<style lang="scss" scoped>
  .login {
    //水平居中
    display: flex;
    justify-content: center;
    //垂直居中
    align-items: center;
    height: 100vh;
  }
</style>
